package org.jeecg.modules.bj.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecg.modules.bj.entity.BjQuit;
import org.jeecg.modules.bj.entity.BjYgruzhi;
import org.jeecg.modules.bj.entity.Ygdab;
import org.jeecg.modules.bj.mapper.BjQuitMapper;
import org.jeecg.modules.bj.mapper.BjYgruzhiMapper;
import org.jeecg.modules.bj.service.IBjQuitService;
import org.jeecg.modules.bj.service.IYgdabService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.ClassUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: bj_quit
 * @Author: jeecg-boot
 * @Date:   2021-10-21
 * @Version: V1.0
 */
@Service
public class BjQuitServiceImpl extends ServiceImpl<BjQuitMapper, BjQuit> implements IBjQuitService {

    @Resource
    IYgdabService ygdabService;
    @Resource
    BjYgruzhiMapper bjYgruzhiMapper;

    @Override
    public void exportFujian(String ygid,HttpServletResponse response) throws Exception{
        Map<String, Object> map = new HashMap<String, Object>();
        Ygdab ygdab=ygdabService.getById(ygid);
        map.put("name",ygdab.getName());
        map.put("sex",ygdab.getSex()==1?"男":"女");
        map.put("dwname",ygdab.getDwname());
        map.put("gwname",ygdab.getGwname());
        map.put("zhiwu",ygdab.getZwgz());
        List<BjYgruzhi> bjYgruzhiList=bjYgruzhiMapper.getListByYgid(ygid);
        if(bjYgruzhiList.isEmpty()==false){
            map.put("ruzhidate",new SimpleDateFormat("yyyy-MM-dd").format(bjYgruzhiList.get(0).getCreateTime()));
        }else{
            map.put("ruzhidate", StringUtils.defaultString(ygdab.getHtstdt(),""));
        }
        String filePath = ClassUtils.getDefaultClassLoader().getResource("fujian.xlsx").getPath();
        TemplateExportParams templateExportParams=new TemplateExportParams();
        templateExportParams.setTemplateUrl(filePath);
        templateExportParams.setScanAllsheet(true);
        Workbook doc = ExcelExportUtil.exportExcel(templateExportParams, map);
        File file=new File("D:\\Users\\file\\附件.xlsx");
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:\\Users\\file\\附件.xlsx");
        doc.write(fos);
        fos.close();
        downloadFile(response,"D:\\Users\\file\\附件.xlsx");
    }

    public void downloadFile(HttpServletResponse response,String filePath) throws IOException {
        File file=new File(filePath);
        // 配置文件下载
        response.setContentType("application/octet-stream");
        // 下载文件能正常显示中文
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
        // 实现文件下载
        byte[] buffer = new byte[1024];
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
            System.out.println("Download the song successfully!");
        }
        catch (Exception e) {
            System.out.println("Download the song failed!");
        }
        finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
